<template>
  <div>
    <div class="node" v-has="{action: 'group:map:list', effect: 'move'}">
      <div :class="[$style.search_box, $style.topSelect]">
      <div :class="$style.selectBox">
          <label>站点：</label>
          <el-select
            style="width:160px"
            v-model="siteId"
            clearable
            @change="getQueryData"
            placeholder="请选择"
          >
            <el-option  v-for="item of siteList"
              :key="item.id"
              :value="item.id"
              :label="item.name"></el-option>
          </el-select>
        </div>
        <div :class="$style.selectBox">
          <label>集团公司：</label>
          <el-select
          filterable
            style="width:160px"
            v-model="companyId"
            clearable
            @change="getQueryData"
            placeholder="请选择"
          >
            <el-option  v-for="item of company"
              :key="item.id"
              :value="item.id"
              :label="item.name"></el-option>
          </el-select>
        </div>
        <div :class="$style.searchBox1" style="margin-left: 20px">
          <el-button type="primary" icon="el-icon-search" @click="getQueryData">查询</el-button>        
        </div>
      </div>
      <Panel title="集团客户站点映射" :intro='intro'>
        <div slot='ext' :class="$style.examine">
          <el-button v-has="{action: 'group:map:add', effect: 'move'}" type="success" icon="el-icon-plus" @click="()=>editStore()">新增</el-button>
      </div>
      <TableEncapsulation
        slot="body"
        :tableConfig="tableConfig"
        :tableData="tableData"
      >
    
      <template slot="operation" slot-scope="operation">
            <el-button
              v-has="{action: 'group:map:update', effect: 'move'}"
              type="text"
              @click="()=>editStore(operation.row.id)"
            >编辑</el-button>
            <el-button
              v-has="{action: 'group:map:delete', effect: 'move'}"
              type="text"
              @click="()=>remove(operation.row.id)"
            >删除</el-button>
          </template>
          <TablePager
            slot="pager"
            v-if="pager.use"
            :size="pager.size"
            :pageChange="pageChange"
            :show-total="pager.showTotal"
            :total="pager.total"
            :current="pager.current"
          >
            <span class="demonstration">GO></span>
          </TablePager>
      </TableEncapsulation>
      </Panel>
    </div>
    <div style="display: none;width:100%;height:100%;margin: auto;" v-has="{action: 'group:map:list', effect: 'noPremiss'}">
      <NoPremission></NoPremission>
    </div>
  </div>
</template>
<script>
import request from "@/tools/request";
import Panel from "@/components/Panel";
import TableEncapsulation from "@/components/TableEncapsulation";
import TablePager from "@/components/Pager";
import SearchSelect from "@/components/SearchSelect";
import { objectFilter } from "@/tools/array";
export default {
  name: "companyMap",
  components: {
    TableEncapsulation,
    TablePager,
    Panel,
    SearchSelect
  },
  data() {
    return {
      intro:{
        title: '',
        content: `绑定集团用户可使用的充电场站`
      },
      siteList:[],
      company:[],
      companyId:'',
      siteId:'',
      tableData: [],
      query: {
        page: 1
      },
       pager: {
        use: true,
        total: 0,
        current: 1,
        size: 10,
        showTotal: true
      },
      name: '',
      operation: true,
    };
  },
  created() {
    if(this.$route.query.id){
      this.companyId= this.$route.query.id
    }
    if(this.$route.query.siteId){
      this.searchs.siteId = this.$route.query.siteId
    }
    this.getQueryData();
  },
  mounted(){
    this.iotSite()
    this.getCompany()
  },
  computed: {
    tableConfig () {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
      return {
        selection: true,
        columnConfig: [
          {
            label: "站点名字",
            type: "text",
            prop: "siteName",
            align: "center"
          },
          {
            label: "集团大客户",
            type: "text",
            prop: "groupName",
            align: "center"
          },
          this.operation && {
            type: "operation",
            align: "center",
            label: "操作",
            width:180
          }
        ].filter(Boolean)
        }
    }
  },
  methods: {
    async getQueryData() {
       let page =  this.pager.current
       let name = this.name
       let siteId = this.siteId
       let companyId = this.companyId
      let search = objectFilter({ name, page, siteId, companyId})
      const res = await request({
        url: "/group/map/list",
        method: "POST",
        data: search
      });
      this.tableData = res.page.list
      this.pager.total = res.page.totalCount
      this.pager.current = res.page.currPage
    },
    pageChange(val) {
      this.pager.current = val;
      this.getQueryData();
    },
    changeSearch(key, val) {
      this[key] = val;
      this.pager.current = 1;
    },
    //获取站点
    async iotSite(){
      let { list } = await request({
      url: "/iot/site/select",
      method: "POST",
      data: {}
      });
      this.siteList= list
    },
     //获取集团公司
    async getCompany(){
      let { list } = await request({
      url: "/company/group/select",
      method: "POST",
      data: {}
      });
      this.company= list
    },
    editStore(id){
      this.$router.push({ path: "/company/map/info",query:{id:id} })
    },


    commentDetail(id){
      this.$router.push({ path: "/storeDetail",query:{id:id,type: "toview"} })
    },
    remove(id){
       this.$confirm('确认删除吗', '操作确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then( async () => {
        let res = await request({
          url: '/group/map/delete',
          method: 'POST',
          dataType: 'json',
          data:[id]
        })
        if(res.code !==0) return
        this.$message.success('删除成功')
        if(this.tableData.length===1 && this.pager.current>1)
          this.$set(this.pager,'current', this.pager.current-1)
        this.getQueryData()
      })
    },
  }
}
</script>
<style lang='scss' module>
.examine{
    display: flex;
    align-items: center;
    :global{
        div{
            color: #fff;
            padding: 10px;
            margin: 0 10px;
            border-radius: 2px;
            font-size: 14px;
            cursor: pointer;
            min-width: 60px;
            text-align: center;
        }
        .adopt{
                background: #409EFF;
        }
    }
}
.search_box {
  display: flex;
  align-items: center;
  flex-wrap: wrap;

  :global {
    .el-input {
      width: 150px;
    }
    .el-select {
      .el-input {
        width: 90px;
      }
    }
    .el-input__icon {
      line-height: 27px;
    }
    .el-range-separator{
      line-height: 26px;
    }
  }
}

.topSelect {
  background-color: white;
  border-radius: 4px;
  padding: 20px;
  margin-bottom: 20px;
}
</style>